//
//  TongJiBaoBiaoListViewController.m
//  YaoHeAPP
//
//  Created by 杨亚森 on 16/6/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "TongJiBaoBiaoListViewController.h"
#import "Masonry.h"

#import "Controller.h"

#import "SVPullToRefresh.h"

#import "labelview.h"
#import "TongYong.h"

#import "NSString+formatWithFloat.h"

#import "QiuGouMingXiModel.h"
#import "QiuGouItemModel.h"

#import "MJExtension.h"

#import "TongJiBaoBiaoItemTableViewCell.h"

#import "StatisticsModel.h"

#define kTongJiBaoBiaoItemTableViewCell @"TongJiBaoBiaoItemTableViewCell"

@interface TongJiBaoBiaoListViewController ()<UITableViewDataSource,UITableViewDelegate>
{

    NSMutableArray *arrayList;//报价数组；
    labelview *la;//无数据显示的页面
    
    QiuGouMingXiModel* _qiuGouMingXiModel;
    
}
@property(nonatomic,strong) UITableView *tableview;  //定义大表格
@property (nonatomic, assign)NSInteger  page;//页数
@end

@implementation TongJiBaoBiaoListViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = bgVCcolor;
    //加一个导航条
    UIView *navcBar = [[UIView alloc] init];
    navcBar.backgroundColor = [UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(60);  make.right.offset(0);
    }];
    
    //导航条标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"统计报表";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(30);
        make.height.offset(28);
        make.top.offset(25);
    }];
    
    //绘制头部信息
    UIView *topView=[[UIView alloc]initWithFrame:CGRectMake(0, 60,SCREEN_WIDTH, 80)];
    topView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:topView];
    
    UIView *fenjiexian=[[UIView alloc]initWithFrame:CGRectMake(0, 40,SCREEN_WIDTH, 0.5)];
    fenjiexian.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    [topView addSubview:fenjiexian];
    
    UIView *fenjiexianbottom=[[UIView alloc]initWithFrame:CGRectMake(0, 79,SCREEN_WIDTH, 0.5)];
    fenjiexianbottom.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    [topView addSubview:fenjiexianbottom];
    
    //顶部label
    UILabel *topLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 20)];
    topLabel.text=[NSString stringWithFormat:@"%@职员求购明细",self.statisticsModel.DATE_TIME];
    topLabel.textAlignment=NSTextAlignmentLeft;
    topLabel.font=[UIFont systemFontOfSize:12];
    topLabel.textColor=[UIColor blackColor];
    [topView  addSubview:topLabel];
    
    
    NSArray *name=@[@"业务员",@"发布/推送单数",@"有报价单数",@"成交单数",@"成交率"];
    
    //间隔
    NSInteger kuan=80;
    NSInteger jiange=(SCREEN_WIDTH-(kuan*5))/6;
    for (int i=0; i<name.count; i++) {
        
        UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(jiange+(jiange+kuan)*i, 50, kuan, 20)];
        Label.text=name[i];
        Label.textAlignment=NSTextAlignmentCenter;
        Label.font=[UIFont systemFontOfSize:11];
        Label.textColor=[UIColor blackColor];
        [topView  addSubview:Label];
    }
    
    
    arrayList=[[NSMutableArray alloc]init];
    
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,140, self.view.frame.size.width, SCREEN_HEIGHT-140) style:UITableViewStylePlain];
    _tableview.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.separatorStyle=NO;
    [self.view addSubview:_tableview];
    
    //放置下拉上拉刷新控件
    __weak TongJiBaoBiaoListViewController *weakSelf = self;
    
    // setup pull-to-refresh
    [self.tableview addPullToRefreshWithActionHandler:^{
        [weakSelf insertRowAtTop];
    }];
    
    //刷新数据
    [self insertRowAtTop];
}

- (void)insertRowAtTop {
    
    __weak TongJiBaoBiaoListViewController *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    
    _page=0;
    NSNumber*number1=[NSNumber numberWithInteger:0];
    NSNumber*number2=[NSNumber numberWithInteger:20];
    NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
    [di setValue:number1 forKey:@"page_num"];//分页,页码
    [di setValue:number2 forKey:@"page_step"];//page_step
    
    NSNumber*date_type=[NSNumber numberWithInteger:self.date_type];
    [di setValue:date_type forKey:@"date_type"];//1:按日汇总,7:按周汇总,30:按月汇总(目前只存在1,7,30三个枚举值)
    [di setValue:self.statisticsModel.START_TIME forKey:@"date_start"];//起始日期,日期格式:2016-01-12
    [di setValue:self.statisticsModel.END_TIME forKey:@"date_end"];//结束日期,日期格式:2016-01-12
    
    
    [Controller requestWithURL:QueryPurchaseStatisticsDetail jsondic:di finishBlock:^(ClassData* data) {
        
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            
            [arrayList removeAllObjects];
            
            
            _qiuGouMingXiModel = [QiuGouMingXiModel mj_objectWithKeyValues:data.data];
            
            
            [arrayList addObjectsFromArray:_qiuGouMingXiModel.LIST];
            
            //判断 是否  有 无数据
            if (arrayList.count==0) {
                if (la) {
                    [la removeFromSuperview];
                }
                la=[[labelview alloc]init];
                [la setimage:@"dingdanNew" tit:@"暂无收支数据" buttontit:nil frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                la.image.frame=CGRectMake((self.view.frame.size.width-75)/2, 50, 75, 100);
                la.label.frame=CGRectMake(50, 150, self.view.frame.size.width-100, 30);
                [_tableview addSubview:la];
                
                
            }else{
                
                if (la) {
                    [la removeFromSuperview];
                }
                [self.tableview reloadData];
                
                
            }
            
            
        }
        else
        {
            
        }
    }];
    
    // 3. 结束刷新(相当于动画)
    [weakSelf.tableview endUpdates];
    
    [weakSelf.tableview.pullToRefreshView stopAnimating];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return arrayList.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TongJiBaoBiaoItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTongJiBaoBiaoItemTableViewCell];
    if (!cell) {
        cell = [[TongJiBaoBiaoItemTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTongJiBaoBiaoItemTableViewCell];
    }
    QiuGouItemModel *qiuGouItemModel = arrayList[indexPath.row];
    cell.qiuGouItemModel = qiuGouItemModel;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger cellindex=indexPath.row;



}

//返回上一层
-(void)backLogin
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
